<template>
  <a-modal class="invoiceHead_modal" v-model="visible" :title="title" :confirmLoading="submitLoading" width="800px"
    ok-text="确认" cancel-text="取消" @ok="addFormSubmit">
    <a-form :form="events_form" layout="inline">
      <div v-if="tabIndex == 1">
        <a-form-item label="企业名称">
          <div>{{ enterpriseName }}</div>
        </a-form-item>
        <a-form-item label="纳税人识别号">
          <div>{{ taxpayerNumber }}</div>
        </a-form-item>
        <a-form-item label="地址">
          <a-input v-decorator="['address', { rules: [{ required: true, message: '请输入地址' }] }]" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input v-decorator="['phone', { rules: [{ required: true, message: '请输入电话' }] }]" />
        </a-form-item>
        <a-form-item label="开户行">
          <a-input v-decorator="['bank', { rules: [{ required: true, message: '请输入开户行' }] }]" />
        </a-form-item>
        <a-form-item label="银行账号">
          <a-input v-decorator="['bankAccount', { rules: [{ required: true, message: '请输入银行账号' }] }]" />
        </a-form-item>
        <a-form-item label="是否默认">
          <a-radio-group v-decorator="['isDefault', { rules: [{ required: true, message: '请选择' }], initialValue: 0 }]">
            <a-radio :value="1">
              是
            </a-radio>
            <a-radio :value="0">
              否
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </div>
      <div v-else>
        <a-form-item label="收件人姓名">
          <a-input v-decorator="['mailName', { rules: [{ required: true, message: '请输入收件人姓名' }] }]" />
        </a-form-item>
        <a-form-item label="收件人电话">
          <a-input v-decorator="['phone', { rules: [{ required: true, message: '请输入收件人电话' }] }]" />
        </a-form-item>
        <a-form-item label="省份">
          <a-input v-decorator="['province', { rules: [{ required: true, message: '请输入省份' }] }]" />
        </a-form-item>
        <a-form-item label="城市">
          <a-input v-decorator="['city', { rules: [{ required: true, message: '请输入城市' }] }]" />
        </a-form-item>
        <a-form-item label="县(区)">
          <a-input v-decorator="['area', { rules: [{ required: true, message: '请输入县(区)' }] }]" />
        </a-form-item>
        <a-form-item label="详细地址">
          <a-input v-decorator="['address', { rules: [{ required: true, message: '请输入详细地址' }] }]" />
        </a-form-item>
        <a-form-item label="是否默认">
          <a-radio-group v-decorator="['isDefault', { rules: [{ required: true, message: '请选择' }], initialValue: 0 }]">
            <a-radio :value="1">
              是
            </a-radio>
            <a-radio :value="0">
              否
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </div>
    </a-form>
  </a-modal>
</template>

<script>
import {
  invoiceTitleQuery,
  invoiceTitleAdd,
  invoiceTitleUpdate,
  invoiceMailQuery,
  invoiceMailAdd,
  invoiceMailUpdate
} from "@/api/human/finance"
import {
  getEnterpriseInfo,
} from "@/api/human/enterprise"
import store from '@/store'
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'AddInfo',
  data() {
    return {
      title: "",
      visible: false,
      submitLoading: false,

      id: "",
      tabIndex: "",
      enterpriseId: "", // 企业id
      enterpriseName: "", // 企业名称
      taxpayerNumber: "", // 统一信用代码
      enterpriseData: [],
    }
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    });
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    // 打开编辑弹窗 
    async add(row, enterpriseId, tab) {
      this.title = row === true ? '新增' : "修改";
      this.visible = true;
      this.tabIndex = tab;
      this.enterpriseId = enterpriseId;
      if (tab == '1') {
        // 抬头 新增|修改
        let { code, data } = await getEnterpriseInfo({ enterpriseId: enterpriseId })
        if (code == '1') {
          this.enterpriseName = data.name;
          this.taxpayerNumber = data.creditCode;
        }
        this.id = row === true ? '' : row.invoiceTitleId;
        if (row === true) {
          // 新增
          this.$nextTick(() => {
            this.events_form.resetFields();
          })
        } else {
          let { code, data } = await invoiceTitleQuery(row.invoiceTitleId)
          let values = this.events_form.getFieldsValue();
          if (code == '1') {
            for (var key in data) {
              values[key] = data[key]
            }
            this.events_form.setFieldsValue(values);
          }
        }
      } else {
        // 邮递 新增|修改
        this.id = row === true ? '' : row.invoiceMailId;
        if (row === true) {
          // 新增
          this.$nextTick(() => {
            this.events_form.resetFields();
          })
        } else {
          let { code, data } = await invoiceMailQuery(row.invoiceMailId)
          let values = this.events_form.getFieldsValue();
          if (code == '1') {
            for (var key in data) {
              values[key] = data[key]
            }
            this.events_form.setFieldsValue(values);
          }
        }
      }
    },
    // 提交
    async addFormSubmit() {
      this.events_form.validateFieldsAndScroll(async (err, values) => {
        if (!err) {
          if (this.tabIndex == '1') {
            // 发票抬头提交
            if (this.enterpriseId) {
              values.enterpriseId = this.enterpriseId;
              values.taxpayerNumber = this.taxpayerNumber;
            }
            if (this.id) {
              values.invoiceTitleId = this.id;
            }
            this.submitLoading = true;
            const { code } = this.id ? await invoiceTitleUpdate(values) : await invoiceTitleAdd(values);
            this.submitLoading = false;
            if (code === '1') {
              this.visible = false;
              this.$message.success('操作成功');
              this.$emit('on-ok');
            }
          } else {
            // 发票邮递提交
            if (this.enterpriseId) {
              values.enterpriseId = this.enterpriseId;
            }
            if (this.id) {
              values.invoiceMailId = this.id;
            }
            this.submitLoading = true;
            const { code } = this.id ? await invoiceMailUpdate(values) : await invoiceMailAdd(values);
            this.submitLoading = false;
            if (code === '1') {
              this.visible = false;
              this.$message.success('操作成功');
              this.$emit('on-ok');
            }
          }
        }
      });
    }
  }
}
</script>
<style lang="less">
.invoiceHead_modal {

  .ant-form-item {
    width: 50%;
    margin-right: 0;
    margin-bottom: 10px;

    .ant-form-item-label {
      width: 40%;
    }

    .ant-form-item-control-wrapper {
      width: 50%;
    }
  }

}
</style>
